package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import model.College;
import model.Teacher;
import dataLayer.DAOTeacher;
import dataLayer.DBconnect;
import types.ConsoleType;
import view.PanelAddTeacher;
import view.PanelHolder;
import view.PanelTeacherList;

public class AddTeacher extends DBconnect implements ActionListener {

	private PanelAddTeacher PATform;

	private String Teacher_code;
	private String Teacher_name;

	public AddTeacher(PanelAddTeacher PATform) {
		this.PATform = PATform;
	}

	// Checks on fields for addTeacher and Error message in console
	public void actionPerformed(ActionEvent e) {

		openConn();
		String message = null;
		ConsoleType type = null;
		Teacher_code = PATform.getJTcode().getText().toUpperCase();
		Teacher_name = PATform.getJTname().getText();
		Teacher t = new Teacher(null, Teacher_code.toUpperCase(), Teacher_name);
		if (e.getActionCommand().equals("Opslaan")) {

			if (Teacher_code.equals("")&&Teacher_name.equals("")) {
				message = "Beide velden zijn nog leeg.";
				type = ConsoleType.EMPTY;
			}
			else if (Teacher_code.equals("")) {
				message = "Er is nog geen docentcode ingevuld.";
				type = ConsoleType.EMPTY;
			}
			else if (Teacher_name.equals("")) {
				message = "Er is nog geen docentnaam ingevuld.";
				type = ConsoleType.EMPTY;
			}
			else if (!College.getInstance().addTeacher(t)) {
				message = "Er bestaat al een docent met deze code.";
				type = ConsoleType.ERROR;
			} else {
			//adds the teacher to the database
				DAOTeacher dbT = DAOTeacher.getInstance();
				dbT.insertTeacher(t);

				PanelHolder ph = PanelHolder.getInstance();
				ph.remove(PATform);
				ph.add(PanelTeacherList.getInstance());
				ph.repaint();
				ph.revalidate();

				message = "Docent " + Teacher_name + " met code "
						+ Teacher_code + " is toegevoegd.";
				type = ConsoleType.SUCCESS;
			}
		}

		// Message naar console
		Console.getInstance().setConsole(message, type);
	}
}